package com.zjt.web;

import com.zjt.service.ICodeCenterService;
import com.zjt.service.IStaticParameterService;
import com.zjt.service.IXmlDocumentService;
import com.zjt.vo.ProductDataVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.IOException;

/**
 * LoginController负责打开登录页面(GET请求)和登录出错页面(POST请求)，
 * <p/>
 * 真正登录的POST请求由Filter完成,
 *
 * @author xueyanbeijing
 */
@Controller
@RequestMapping(value="/web/productDataSource")
public class BarCodeController {
    private static Logger logger = LoggerFactory.getLogger(BarCodeController.class);
    private IXmlDocumentService xmlDocumentService;
    private ICodeCenterService codeCenterService;
    private IStaticParameterService staticParameterService;
    @Autowired
    public void setXmlDocumentService(IXmlDocumentService xmlDocumentService) {
        this.xmlDocumentService = xmlDocumentService;
    }
    @Autowired
    public void setCodeCenterService(ICodeCenterService codeCenterService) {
        this.codeCenterService = codeCenterService;
    }
    @Autowired
    public void setStaticParameterService(IStaticParameterService staticParameterService) {
        this.staticParameterService = staticParameterService;
    }

    @RequestMapping(value = "/getData", method = RequestMethod.GET)
    public String getData(RedirectAttributes redirectAttributes) throws IOException{
            String urlPath = staticParameterService.getParameterValueByName("getProductDataByBarCodeUrl");
            String clientID = staticParameterService.getParameterValueByName("clientID");
            String url = urlPath +"?clientID="+ clientID+"&barcode=6907260166061" ;
            try
            {
                ProductDataVO productDataVO = xmlDocumentService.getProductDataFromHttpGet(url);
                codeCenterService.saveCenterProduct(productDataVO.getGtin(),productDataVO.getItemName(),productDataVO.getBrandName(),productDataVO.getSpecification(),
                        productDataVO.getShortDescription(),productDataVO.getDescription(),productDataVO.getHeight() ,productDataVO.getWidth(),productDataVO.getDepth(),
                        productDataVO.getNetContent(),productDataVO.getGrossWeight(),productDataVO.getNetWeight(),productDataVO.getPackagingTypeCode(),
                        productDataVO.getPackagingMaterialCode(),productDataVO.getClassCode() ,productDataVO.getExtension(),
                        productDataVO.getFirmName(),productDataVO.getAddress(),productDataVO.getFirmExtension(),0);
            }catch (Exception e){
                e.printStackTrace();
                logger.error("载入物码中心数据异常"+e.getMessage());
            }
        redirectAttributes.addFlashAttribute("message", "下载barcode成功");
        return "redirect:staticparameter/parameterList";
    }
}
